package com.cxx.home.controller;

import com.cxx.common.ReadView;
import com.cxx.common.WriteView;
import com.cxx.home.dto.ledger.LedgerDto;
import com.cxx.home.service.LedgerService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ledger")
@Tag(name = "账本")
public class LedgerController {
    @Resource
    private LedgerService ledgerService;

    @Operation(summary = "查询账本内容")
    @GetMapping("")
    public @JsonView(ReadView.class) List<LedgerDto> queryLedger(@RequestParam("book") String book) {
        return ledgerService.queryLedger(book);
    }

    @Operation(summary = "新增账本内容")
    @PostMapping("")
    public Boolean addLedger(@RequestBody @JsonView(WriteView.class) @Validated LedgerDto ledgerDto) {
        return ledgerService.addLedger(ledgerDto);
    }

    @Operation(summary = "更新账本内容")
    @PutMapping("/{id}")
    public Boolean updateLedger(@PathVariable("id") long id,
                                @RequestBody @JsonView(WriteView.class) LedgerDto ledgerDto) {
        return ledgerService.updateLedger(id, ledgerDto);
    }

    @Operation(summary = "删除账本内容")
    @DeleteMapping("/{id}")
    public Boolean deleteLedger(@PathVariable("id") long id) {
        return ledgerService.deleteLedger(id);
    }

    @Operation(summary = "查询账本")
    @GetMapping("/book")
    public List<String> queryLedgerBook() {
        return ledgerService.queryLedgerBook();
    }
}
    